package org.cupk2.service.event;

import com.cupk2.lucene.Cupk2LuceneFace;
import com.cupk2.lucene.document.CupkDocument;
import com.cupk2.lucene.impl.Cupk2LuceneIndexImpl;
import com.cupk2.lucene.inter.Cupk2LuceneIndexInter;
import com.cupk2.lucene.utils.CupkLuceneUtils;
import lombok.extern.slf4j.Slf4j;
import org.cupk2.base.event.F2Event;
import org.cupk2.base.event.enums.F2EActionT;
import org.cupk2.base.event.enums.F2EObjectT;
import org.cupk2.base.event.inter.Cupk2EventHandle;
import org.cupk2.files.domain.ResourceFile;
import org.cupk2.files.service.ResourceFileServiceInter;
import org.cupk2.files.utils.Cupk2RegisteTypeEnum;
import org.cupk2.files.utils.FileDownloadUtils;
import org.cupk2.service.event.utils.KnowIndexDocumentBuilder;
import org.cupk2.service.inter.CupkUserServiceInter;
import org.cupk2.service.lucene.doc.KnowIndexDocument;
import org.cupk2.service.lucene.doc.enums.DocumentTitleEnum;
import org.cupk2.zhzm.domain.ZhzmKnow;
import org.cupk2.zhzm.service.ZhzmKnowServiceInter;
import org.springframework.beans.factory.annotation.Autowired;
import org.cupk2.wdap.service.WdapExtendFileServiceInter;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 创建附件索引
 */
@Slf4j
@Service
public class EventLuceneIndexHandle implements Cupk2EventHandle {
    @Autowired
    private ResourceFileServiceInter resourceFileServiceImpl;
    @Autowired
    private FileDownloadUtils fileDownloadUtils;
    @Autowired
    private WdapExtendFileServiceInter wdapExtendFileServiceImpl;
    @Autowired
    private CupkUserServiceInter cupkUserService;
    @Autowired
    private ZhzmKnowServiceInter zhzmKnowServiceImpl;
    @Autowired
    private KnowIndexDocumentBuilder knowIndexDocumentBuilder;


    @Override
    public void handle(F2Event event) {
        if (event.getAction().getType().equals(F2EActionT.UPDATE) && event.getObj().getType().equals(F2EObjectT.KNOW)) {
            String knowId = event.getObj().getId();

            Cupk2LuceneIndexInter indexService = Cupk2LuceneFace.getIndexService();
            indexService.deleteIndex(List.of(knowId));
            ZhzmKnow know = zhzmKnowServiceImpl.getZhzmKnowById(knowId);

            indexKnow(knowId, know);

            List<String> files = zhzmKnowServiceImpl.getInvalidVersionFileIds(know);
            indexService.deleteIndex(files);
        }
        if (event.getAction().getType().equals(F2EActionT.DELETE) && event.getObj().getType().equals(F2EObjectT.KNOW)) {

            String knowId = event.getObj().getId();
            Cupk2LuceneIndexInter indexService = new Cupk2LuceneIndexImpl(CupkLuceneUtils.getLuceneIndexFilePath());
            indexService.deleteIndex(List.of(knowId), DocumentTitleEnum.knowId.name());
        }
        if (event.getAction().getType().equals(F2EActionT.ADD) && event.getObj().getType().equals(F2EObjectT.KNOW)) {

            String knowId = event.getObj().getId();
            ZhzmKnow know = zhzmKnowServiceImpl.getZhzmKnowById(knowId);
            if (know != null) {
                indexKnow(knowId, know);
            }
        }

        if (event.getAction().getType().equals(F2EActionT.EXTEND_FILE)) {
            try {
                String fileId = event.getObj().getId();
                ResourceFile rFile = resourceFileServiceImpl.getResourceFileById(fileId);
                String knowId = resourceFileServiceImpl.getBindAppId(rFile.getId(), Cupk2RegisteTypeEnum.KNOW);
                Cupk2LuceneIndexInter indexService = new Cupk2LuceneIndexImpl(CupkLuceneUtils.getLuceneIndexFilePath());
                indexService.deleteIndex(List.of(fileId));
                List<CupkDocument> docs = new ArrayList<>();
                CupkDocument doc = knowIndexDocumentBuilder.getFileDoc(knowId, fileId).getDoc();
                if (doc != null) {
                    docs.add(doc);
                }
                indexService.createIndex(docs);
            } catch (IOException e) {
                log.error(e.getMessage(), e);
            }
        }

        if (event.getAction().getType().equals(F2EActionT.CANCEL_FILE)) {
            String fileId = event.getObj().getId();
            Cupk2LuceneIndexInter indexService = new Cupk2LuceneIndexImpl(CupkLuceneUtils.getLuceneIndexFilePath());
            indexService.deleteIndex(List.of(fileId));
        }
    }

    private void indexKnow(String knowId, ZhzmKnow know) {
        Cupk2LuceneIndexInter indexService = new Cupk2LuceneIndexImpl(CupkLuceneUtils.getLuceneIndexFilePath());
        indexService.deleteIndex(List.of(knowId));
        List<CupkDocument> docs = new ArrayList<>();
        KnowIndexDocument doc = knowIndexDocumentBuilder.getKnowDoc(knowId);
        if (doc != null) {
            docs.add(doc.getDoc());
        }
        indexService.createIndex(docs);
    }

}
